import { getLocalStorage } from '@/utils/common.js'

const { collapse } = getLocalStorage('collapse')

let timeId
export const useSettingsStore = defineStore({
  id: 'settings',
  state: () => ({
    collapse,
    device: 'desktop',
    reload: true,
    pageLoading: false,
  }),
  getters: {
    getPageLoading(state) {
      return state.pageLoading
    },
  },
  actions: {
    toggleCollapse() {
      this.collapse = !this.collapse
      localStorage.setItem('collapse', JSON.stringify({ collapse: this.collapse }))
    },
    foldSideBar() {
      this.collapse = true
    },
    toggleDevice(device) {
      this.device = device
    },
    toggleReload() {
      this.reload = !this.reload
    },
    setPageLoading(loading) {
      this.pageLoading = loading
    },
    setPageLoadingAction(loading) {
      if (loading) {
        clearTimeout(timeId)
        timeId = setTimeout(() => {
          this.setPageLoading(loading)
        }, 50)
      } else {
        this.setPageLoading(loading)
        clearTimeout(timeId)
      }
    },
  },
})
